<?php
namespace App\Http\Controllers\ConsoleWeb;

use App\Http\Controllers\Business\Admin\Account\AgentAccountManagement;
use App\Http\Controllers\Business\Admin\Common\Common;
use App\Http\Controllers\Business\Shop\V1\Account\Account;
use App\Http\Controllers\Business\Shop\V1\Admin\Admin;
use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\Admin\Account\AccountManagement;
use App\Http\Controllers\Business\Admin\Account\SalesmanManagement;
use App\Models\AdminUser;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;

class FinanceController extends WebController
{
    //资金平台流水
    public function platformMoney() {
        $cityId = $this->request->get('cityid');
        $startTime = '';
        $lastTime = '';
        $result = AccountManagement::ledgerAccount($startTime, $lastTime);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $data['cityid'] = $cityId;
        return view('console.finance.platform_money', $data);
    }
    //后台充值
    public function rechargeMoney() {
        $mobile = $this->request->get('mobile');
        $money = $this->request->get('money');
        $note = $this->request->get('note');
        $password = $this->request->get('password');
        if(env('WEB_URL') == 'https://www.fbw-china.com/') {
            if ($password != "fbw1984") {
                return $this->responseError("密码错误");
            }
        } else {
            if ($password != "fbw123") {
                return $this->responseError("密码错误");
            }
        }
        $result = AccountManagement::userRecharge($mobile, $money, $note);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //退款
    public function refund() {
        $mobile = $this->request->get('mobile');
        $money = $this->request->get('money');
        $fee = $this->request->get('fee');
        $note = $this->request->get('note');
        $password = $this->request->get('password');
        if(env('WEB_URL') == 'https://test2.fbw-china.com/') {
            if($password != 'fbw123') {
                return $this->responseError('密码错误');
            }
        } else {
            if($password != 'fbw1984') {
                return $this->responseError('密码错误');
            }
        }
        $result = AccountManagement::refund($mobile, $money, $fee, $note);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //输入手机号码获取最近几笔充值记录
    public function getRechargeByMobile() {
        $mobile = $this->request->get('mobile');
        $take = 3;
        $skip = 0;
        $result = AccountManagement::getRechargeByMobile($mobile, $skip, $take);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //获取昵称
    public function getNicknameByMobile() {
        $mobile = $this->request->get('mobile');

    }
    //用户对账单
    public function userStatement() {
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $perpage = 10000;
        $result = AccountManagement::newUserStatement($startTime, $lastTime, $perpage);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $cityId = $this->request->get('cityid');
        $data['cityid'] = $cityId;
        $data['starttime'] = $startTime;
        $data['lasttime'] = $lastTime;
        return view('console.finance.user_statement', $data);
    }
    //导出用户对帐单
    public function exportUserStatement() {
        $startTime = $this->request->get('starttime');
        $lastTime = $this->request->get('lasttime');
        $result = AccountManagement::exportUserStatement($startTime, $lastTime);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        $title = ["用户昵称","用户帐号","充值金额(元)","已到账金额","待到账金额","实付金额","可用余额"];
        $cellData = [];
        array_push($cellData, $title);
        foreach($data as $list) {
            $newList = [
                'nickname' => $list['nickname'],
                'mobile' => $list['mobile'],
                'rechargemoney' => $list['rechargemoney'],
                'isdouble' => $list['isdouble'],
                'notdouble' => $list['notdouble'],
                'actualamount' => $list['actualamount'],
                'balance' => $list['balance']
            ];
            array_push($cellData, $newList);
        }
        $title = '用户对账单';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('用户对账单', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //充值明细
    public function rechargeDetails() {
        $cityId = $this->request->get('cityid');
        $userId = $this->request->get('userid');
        $nickName = $this->request->get('nickname');
        $result = AccountManagement::userRechargeList($userId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $data['cityid'] = $cityId;
        $data['nickname'] = $nickName;
        return view('console.finance.recharge_details', $data);
    }
    //消费明细
    public function financeConsumeDetails() {
        $cityId = $this->request->get('cityid');
        $userId = $this->request->get('userid');
        $result = AccountManagement::userOrderList($userId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $data['cityid'] = $cityId;
        return view('console.finance.consume_details', $data);
    }
    //收款账号管理
    public function receivableManage() {
        $cityId = $this->request->get('cityid');
        $perpage = "10000";
        $result = AccountManagement::shopBankManagement($perpage,$cityId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $data = [
            'lists' => $lists,
            'cityid' => $cityId
        ];
        return view('console.finance.receivable_manage', $data);
    }
    //收款账号管理修改页面
    public function receivableCheck() {
        $cityId = $this->request->get('cityid');
        $shopId = $this->request->get('shopid');
        $result = AccountManagement::getShopBank($shopId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $data['cityid'] = $cityId;
        return view('console.finance.receivable_check', $data);
    }
    //收款账号管理修改页面
    public function updateReceivable() {
        $input = $this->request->input();
        $shopId = $input['shopid'];
        $cardholder = $input['cardholder'];
        $bankcard = $input['bankcard'];
        $bankName = $input['bankname'];
        $bankProvince = $input['bankprovince'];
        $bankCity = $input['bankcity'];
        $openCardBank = $input['opencardbank'];
        $idcard = $input['idcard'];
        $result = AccountManagement::editShopBank($shopId, $cardholder, $bankcard, $bankName, $bankProvince, $bankCity, $openCardBank,$idcard);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //商家对账单
    public function businessStatement() {
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $perPage = 50;
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        $cityId = $this->request->get('cityid');
        $statisticResult = AccountManagement::shopStatementStatistic($cityId, $startTime, $lastTime, $search);
        if($statisticResult['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $statisticResult['data'];
        $listResult = AccountManagement::shopStatementList($cityId, $startTime, $lastTime, $page, $perPage, $search);
        if($listResult['status'] == 'failed') {
            return view('errors.404');
        }
        $data['cityid'] = $cityId;
        $data['starttime'] = $startTime;
        $data['lasttime'] = $lastTime;
        $data['lists'] = $listResult['data'];
        $data['search'] = $search;
        $data['starttime'] = $startTime;
        $data['lasttime'] = $lastTime;
        $data['lastpage'] = ceil($data['shopnum'] / $perPage);
        $data['page'] = $page;
        return view('console.finance.business_statement', $data);
    }
    //导出商家对账单
    public function exportBusinessStatement() {
        $startTime = $this->request->get('starttime');
        $lastTime = $this->request->get('lasttime');
        $result = AccountManagement::exportShopStatement($startTime, $lastTime);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        $title = ["商家代码","商家名称","一级分类","二级分类","收银金额","商家红包","结款金额(元)", "已提现金额(元)", "待提现金额(元)"];
        $cellData = [];
        array_push($cellData, $title);
        foreach($data as $list) {
            $newList = [
                'shopcode' => $list['shopcode'],
                'shopname' => $list['shopname'],
                'firstclassname' => $list['firstclassname'],
                'secondclassname' => $list['secondclassname'],
                'shopamount' => $list['shopamount'],
                'redenvelopeamount' => $list['redenvelopeamount'],
                'accountamount' => $list['accountamount'],
                'iswithdraw' => $list['iswithdraw'],
                'notwithdraw' => $list['notwithdraw']
            ];
            array_push($cellData, $newList);
        }
        $title = '商家对账单';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('商家对账单', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //商家对账明细
    public function businessStatementDetails() {
        $cityId = $this->request->get('cityid');
        $shopId = $this->request->get('shopid');
        $startTime = $this->request->get('starttime');
        $startTime = date('Y-m-d', strtotime($startTime)) . " 00:00:00";
        $lastTime = $this->request->get('lasttime');
        $lastTime = date('Y-m-d', strtotime($lastTime)) . " 23:59:59";
        $result = AccountManagement::shopOrderList($shopId, $startTime, $lastTime);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $data['cityid'] = $cityId;
        $data['starttime'] = $startTime;
        $data['lasttime'] = $lastTime;
        return view('console.finance.business_statement_details', $data);
    }
    //商家待提现列表
    public function waitMoneyList() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        //更新商家待提现的数据，并获取商家待提现笔数和金额
        $countResult = AccountManagement::shopWaitWithdrawCount($search,$cityId);
        if($countResult['status'] == 'failed'){
            return view('errors.404');
        }
        $count = $countResult['data']['count'];
        $shopAmount = $countResult['data']['shopamount'];
        $failedCount = $countResult['data']['failedcount'];
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $length = 50;
        $result = AccountManagement::shopWaitWithdrawList($search, $page, $length,$cityId);
        if ($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $lastPage = ceil($count / $length);
        $data = [
            'cityid' => $cityId,
            'count' => $count,
            'shopamount' => $shopAmount,
            'failedcount' => $failedCount,
            'page' => $page,
            'lastpage' => $lastPage,
            'perpage' => $length,
            'lists' => $lists
        ];
        return view('console.finance.waitmoney_list', $data);
    }
    //将待提现的金额打款
    public function waitMoneyRemit() {
        $shopAccountId = $this->request->get('shopaccountid');
        $cityId = $this->request->get('cityid');
        $result = AccountManagement::shopWithdraw($shopAccountId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage('打款成功');
        }
    }
    //刷新
    public function refreshWithdrawStatus() {
        $result = AccountManagement::shopRefreshWithdrawStatus();
        if($result['status'] == 'failed') {
            return $this->responseError('刷新失败');
        }
        return $this->responseSuccessMessage($result['message']);
    }
    //导出商家待提现列表
    public function exportWaitMoney() {
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $cityId = $this->request->get('cityid');
        $result = AccountManagement::shopStartWithdraw($cityId);
        if($result['status'] == 'failed') {
            return $this->responseError('导出失败');
        }
        $data = $result['data'];
        $title = ["收款账户列", "收款户名列", "转账金额列", "备注列", "收款银行列", "收款银行支行列", "收款省/直辖市列", "收款市县列"];
        $cellData = [];
        array_push($cellData, $title);
        foreach($data as $list) {
            $newList = [
                //'bankcard' => ($list['bankcard'] != '')? number_format($list['bankcard']) : '',
                'bankcard' => $list['bankcard']. " ",
                'cardholder' => $list['cardholder'],
                'sumaccoutamount' => $list['accountamount'],
                'code' => $list['code'],
                'bankname' => $list['bankname'],
                'opencardbank' => $list['opencardbank'],
                'bankprovince' => $list['bankprovince'],
                'bankcity' => $list['bankcity'],
            ];
            array_push($cellData, $newList);
        }
        $title = '待提现';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('待提现', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //商家已提现列表
    public function gotmoneyList() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $search = '%';
        $result = AccountManagement::shopAlreadyWithdraw($startTime, $lastTime, $search,$cityId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $data['cityid'] = $cityId;
        $data['starttime'] = $startTime;
        $data['lasttime'] = $lastTime;
        return view('console.finance.gotmoney_list', $data);
    }
    //导出商家已提现
    public function exportGotMoneyList() {
        $startTime = $this->request->get('starttime');
        $lastTime = $this->request->get('lasttime');
        $cityId = $this->request->get('cityid');
        $result = AccountManagement::shopAlreadyWithdraw($startTime, $lastTime,'',$cityId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data']['data'];
        $title = ["商家代码", "商家名称", "持卡人", "银行卡号", "银行名称", "开户行所在地", "开户行行名称", "提现日", "提现金额(元)"];
        $cellData = [];
        array_push($cellData, $title);
        foreach($data as $list) {
            $newList = [
                'shopcode' => $list['shopcode'],
                'shopname' => $list['shopname'],
                'cardholder' => $list['cardholder'],
                'bankcard' => $list['bankcard'] . ' ',
                'bankname' => $list['bankname'],
                'bankprovince' => $list['bankprovince'] .  $list['bankcity'],
                'opencardbank' => $list['opencardbank'],
                'accounttime' => date("Y-m-d H:i:s",strtotime($list['accounttime'])),
                'accountamount' => $list['accountamount']
            ];
            array_push($cellData, $newList);
        }
        $title = '已提现';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('已提现', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //商务月账单
    public function monthList() {
        return view('console.finance.month_list');
    }
    //商务明细
    public function monthListDetails() {
        return view('console.finance.month_list_details');
    }
    //地推对账单首页
    public function salesmanStatementIndex() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('salesmantype')) {
            $salesmanType = $this->request->get('salesmantype');
        } else {
            $salesmanType = 'shopsalesman';
        }
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
            $type = '';
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        if($salesmanType == 'shopsalesman') {
            //商家地推
            $shopStatisticResult = SalesmanManagement::shopSalesmanStatementStatistic($cityId);
            if ($shopStatisticResult['status'] == 'failed') {
                return view('errors.404');
            }
            $statistic = $shopStatisticResult['data'];
            $shopListsResult = SalesmanManagement::shopSalesmanStatementList($cityId, $startTime, $lastTime);
            if($shopListsResult['status'] == 'failed') {
                return view('errors.404');
            }
            $lists = $shopListsResult['data'];
        } else {
            //用户地推
            $userStatisticResult = SalesmanManagement::userSalesmanStatementStatistic($cityId);
            if ($userStatisticResult['status'] == 'failed') {
                return view('errors.404');
            }
            $statistic = $userStatisticResult['data'];
            $userListsResult = SalesmanManagement::userSalesmanStatementList($cityId, $startTime, $lastTime);
            if(!$userListsResult['status'] == 'failed') {
                return view('errors.404');
            }
            $lists = $userListsResult['data'];
        }
        $data = [
            'salesmantype' => $salesmanType,
            'type' => $type,
            'starttime' => $startTime,
            'lasttime' => $lastTime,
            'statistic' => $statistic,
            'lists' => $lists,
            'cityid' => $cityId
        ];
        return view('console.finance.salesman_shopstatement', $data);
    }
    //地推待提现首页
    public function salesmanWaitWithdrawalIndex() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('salesmantype')) {
            $salesmanType = $this->request->get('salesmantype');
        } else {
            $salesmanType = 'shopsalesman';
        }
        if($salesmanType == 'shopsalesman') {
            //准备商家地推待提现数据
            //SalesmanManagement::readyShopSalesmanWaitWithdrawal($cityId);
            $shopStatisticResult = SalesmanManagement::shopSalesmanWaitWithdrawalStatistic($cityId);
            if ($shopStatisticResult['status'] == 'failed') {
                return view('errors.404');
            }
            $statistic = $shopStatisticResult['data'];
            $shopListsResult = SalesmanManagement::shopSalesmanWaitWithdrawalList($cityId);
            if($shopListsResult['status'] == 'failed') {
                return view('errors.404');
            }
            $lists = $shopListsResult['data'];
        } else {
            //准备用户地推待提现数据
            SalesmanManagement::readyUserSalesmanWaitWithdrawal($cityId);

            $userStatisticResult = SalesmanManagement::userSalesmanWaitWithdrawalStatistic($cityId);
            if ($userStatisticResult['status'] == 'failed') {
                return view('errors.404');
            }
            $statistic = $userStatisticResult['data'];
            $userListsResult = SalesmanManagement::userSalesmanWaitWithdrawalList($cityId);
            if($userListsResult['status'] == 'failed') {
                return view('errors.404');
            }
            $lists = $userListsResult['data'];
        }
        $data = [
            'salesmantype' => $salesmanType,
            'cityid' => $cityId,
            'statistic' => $statistic,
            'lists' => $lists
        ];
        return view('console.finance.salesman_shopwaitwithdrawal', $data);
    }
    //地推已提现首页
    public function salesmanWithdrawalIndex() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        if($this->request->has('salesmantype')) {
            $salesmanType = $this->request->get('salesmantype');
        } else {
            $salesmanType = 'shopsalesman';
        }

        if($salesmanType == 'usersalesman') {
            $result = SalesmanManagement::userSalesmanWithdrawalList($cityId, $startTime, $lastTime);
        } else {
            $result = SalesmanManagement::shopSalesmanWithdrawalList($cityId, $startTime, $lastTime);
        }
        if($result['status'] == 'failed') {
            return view('errors.404');
        } else {
            $data = $result['data'];
            return view('console.finance.salesman_shopwithdrawal', $data);
        }
    }
    //导出地推对账单
    public function exportSalesmanStatement() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('salesmantype')) {
            $salesmanType = $this->request->get('salesmantype');
        } else {
            $salesmanType = 'shopsalesman';
        }
        $startTime = $this->request->get('starttime');
        $lastTime = $this->request->get('lasttime');
        if($salesmanType == 'shopsalesman') {
            //商家地推
            $result = SalesmanManagement::exportShopSalesmanStatement($cityId, $startTime, $lastTime);
            if($result['status'] == 'failed') {
                return $this->responseError($result['message']);
            }
            $data = $result['data'];
            $cellData = [
                ['创建时间','商家代码','商家名称','地推类型','注册用户数','订单数','地推订单数','地推订单实付','收入','已提现金额','余额']
            ];
            foreach($data as $list) {
                $newList = [
                    'created_at'=>$list['created_at'],
                    'shopcode' => $list['shopcode'],
                    'shopname' => $list['shopname'],
                    'salesmantype' => ($list['salesmantype']=='common')?'普通地推':'合伙人地推',
                    'registernum' => $list['registernum'],
                    'ordernum' => $list['ordernum'],
                    'validordernum' => $list['validordernum'],
                    'totalorderamount' => $list['totalorderamount'],
                    'getmoney' => $list['getmoney'],
                    'withdrawalmoney' => $list['withdrawalmoney'],
                    'balance' => $list['balance']
                ];
                array_push($cellData, $newList);
            }
        } else {
            //用户地推
            $result = SalesmanManagement::exportUserSalesmanStatement($cityId, $startTime, $lastTime);
            if($result['status'] == 'failed') {
                return $this->responseError($result['message']);
            }
            $data = $result['data'];
            $cellData = [
                ['创建时间','个人地推帐号','姓名','注册用户数','订单数','地推订单数','地推订单实付','收入','已提现金额','余额', '状态']
            ];
            foreach($data as $list) {
                $newList = [
                    'created_at' => $list['created_at'],
                    'mobile' => $list['mobile'],
                    'name'  => $list['name'],
                    'registernum'  => $list['registernum'],
                    'ordernum'  => $list['ordernum'],
                    'ordervalidnum' => $list['ordervalidnum'],
                    'ordertotalamount' => $list['ordertotalamount'],
                    'totalmoney' => $list['totalmoney'],
                    'withdrawalmoney' => $list['withdrawalmoney'],
                    'balance' => $list['balance'],
                    'status' => ($list['status'] ==1) ? "可用" : "不可用"
                ];
                array_push($cellData, $newList);
            }
        }
        $title = '地推对账单';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('地推对账单', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //导出待提现首页
    public function exportSalesmanWaitWithdrawal() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('salesmantype')) {
            $salesmanType = $this->request->get('salesmantype');
        } else {
            $salesmanType = 'shopsalesman';
        }
        if($salesmanType == 'shopsalesman') {
            $result = SalesmanManagement::exportShopSalesmanWaitWithdrawal($cityId);
            if($result['status'] == 'failed') {
                return $this->responseError($result['message']);
            }
            $data = $result['data'];
            $cellData = [
                ['商家代码','商家名称','持卡人', '银行卡号', '银行名称', '省份', '城市', '开户行', '待提现金额']
            ];
            foreach($data as $list) {
                $newList = [
                    'shopcode' => $list['shopcode'],
                    'shopname' => $list['shopname'],
                    'cardholder' => $list['cardholder'],
                    'bankcard' => $list['bankcard'],
                    'bankname' => $list['bankname'],
                    'bankprovince' => $list['bankprovince'],
                    'bankcity' => $list['bankcity'],
                    'opencardbank' => $list['opencardbank'],
                    'accountamount' => $list['accountamount']
                ];
                array_push($cellData, $newList);
            }
        } else {
            $result = SalesmanManagement::exportUserSalesmanWaitWithdrawal($cityId);
            if($result['status'] == 'failed') {
                return $this->responseError($result['message']);
            }
            $data = $result['data'];
            $cellData = [
                ['个人地推帐号', '手机号码', '待提现金额']
            ];
            foreach($data as $list) {
                $newList = [
                    'name' => $list['name'],
                    'mobile' => $list['mobile'],
                    'accountmoney' => $list['accountmoney']
                ];
                array_push($cellData, $newList);
            }
        }

        $title = '地推待提现';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('地推待提现', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //导出已提现首页
    public function exportSalesmanWithdrawal() {
        $cityId = $this->request->get('cityid');
        $startTime = $this->request->get('starttime');
        $lastTime = $this->request->get('lasttime');
        if($this->request->has('salesmantype')) {
            $salesmanType = $this->request->get('salesmantype');
        } else {
            $salesmanType = 'shopsalesman';
        }
        if($salesmanType == 'usersalesman') {
            $result = SalesmanManagement::exportUserSalesmanWithdrawal($cityId, $startTime, $lastTime);
            if($result['status'] == 'failed') {
                return $this->responseError($result['message']);
            }
            $data = $result['data'];
            $cellData = [
                ['个人地推帐号', '姓名', '提现时间', '提现金额']
            ];
            foreach($data as $list) {
                $newList = [
                    'mobile' => $list['mobile'],
                    'name' => $list['name'],
                    'accounttime' => $list['accounttime'],
                    'accountmoney' => $list['accountmoney'],
                ];
                array_push($cellData, $newList);
            }
        } else {
            $result = SalesmanManagement::exportShopSalesmanWithdrawal($cityId, $startTime, $lastTime);
            if($result['status'] == 'failed') {
                return $this->responseError($result['message']);
            }
            $data = $result['data'];
            $cellData = [
                ['商家代码','商家名称','持卡人','银行卡', '银行名称', '省份', '城市', '开户行', '提现时间','提现金额']
            ];
            foreach($data as $list) {
                $newList = [
                    'shopcode' => $list['shopcode'],
                    'shopname' => $list['shopname'],
                    'cardholder' => $list['cardholder'],
                    'bankcard' => $list['bankcard'],
                    'bankname' => $list['bankname'],
                    'bankprovince' => $list['bankprovince'],
                    'bankcity' => $list['bankcity'],
                    'opencardbank' => $list['opencardbank'],
                    'accounttime' => $list['accounttime'],
                    'accountamount' => $list['accountamount']
                ];
                array_push($cellData, $newList);
            }
        }

        $title = '地推已提现';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('地推已提现', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //给地推商家打款
    public function remitSalesmanShop() {
        $cityId = $this->request->get('cityid');
        $withdrawalId = $this->request->get('withdrawalid');
        if($this->request->has('remark')) {
            $remark = $this->request->get('remark');
        } else {
            $remark = '';
        }
        $result = SalesmanManagement::remitSalesmanShop($withdrawalId, $remark);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessMessage($result['message']);
    }
    //给用户地推打款
    public function remitSalesmanUser() {
        $cityId = $this->request->get('cityid');
        $withdrawalId = $this->request->get('withdrawalid');
        $result = SalesmanManagement::remitSalesmanUser($withdrawalId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessMessage($result['message']);
    }
    //代理商分润
    public function agentProfit() {
        $cityId = $this->request->get('cityid');
        $result = AgentAccountManagement::agentProfit($cityId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        return view('console.finance.agent_profit', $data);
    }
    //导出代理商分润
    public function exportAgentProfit() {
        $cityId = $this->request->get('cityid');
        $result = AgentAccountManagement::exportAgentProfit($cityId);
        $title = '利润分成明细表';
        $cellData = [
            ['日期','订单消费金额(元)','[代理商]服务费收益(元)','[代理商]已使用的红包(元)', '收入']
        ];
        foreach($result['data'] as $list) {
            $newList = [
                'accounttime' => $list['accounttime'],
                'orderamount' => $list['orderamount'],
                'fee' => $list['fee'],
                'redenveloperefundamount' => $list['redenveloperefundamount'],
                'profit' => $list['profit'],
            ];
            array_push($cellData, $newList);
        }
        $title = '代理商分润列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('代理商分润列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //获取某个月的代理商分润
    public function agentProfitByMonth() {
        $cityId = $this->request->get('cityid');
        $months = $this->request->get('months');
        $result = AgentAccountManagement::agentProfitByMonth($cityId,$months);
        if($result['status'] == 'failed'){
            return view('errors.404');
        }
        $data['lists'] = $result['data'];
        $data['months'] = $months;
        $data['cityid'] = $cityId;
        return view('console.finance.agent_profit_by_month',$data);
    }
    //导出某个月的代理商分润
    public function exportAgentProfitByMonth() {
        $cityId = $this->request->get('cityid');
        $months = $this->request->get('months');
        $result = AgentAccountManagement::exportAgentProfitByMonth($cityId,$months);
        $title = '代理商当月分润列表';
        $cellData = [
            ['日期','订单消费金额(元)','[代理商]服务费收益(元)','[代理商]已使用的红包(元)', '收入']
        ];
        foreach($result['data'] as $list) {
            $newList = [
                'accounttime' => $list['accounttime'],
                'orderamount' => $list['orderamount'],
                'fee' => $list['fee'],
                'redenveloperefundamount' => $list['redenveloperefundamount'],
                'profit' => $list['profit'],
            ];
            array_push($cellData, $newList);
        }
        $title = '代理商当月分润列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('代理商当月分润列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //代理商收款账号管理
    public function agentReceivableManage() {
        $cityId = $this->request->get('cityid');
        $provincename = $this->request->get('provincename');
        if(!empty($provincename)){
           // 获取城市信息
            $city = AgentAccountManagement::getBankCity($provincename);
            return $this->responseSuccessData($city['data']);
        }
        //获取所有城市的代理商信息
        $result = AgentAccountManagement::agentReceivableManage();
        //获取省份名称
        $province = AgentAccountManagement::getBankProvince();
        $data['lists'] = $result['data'];
        $data['bankprovince'] = $province['data'];
        $data['cityid'] = $cityId;
        return view('console.finance.agent_receivable_manage',$data);
    }
    //对代理商收款账号进行修改
    public function agentReceivableManageEdit(){
        $input = $this->request->input();
        $adminid = $input['adminid'];
        $data = [
            'id'=>$input['adminid'],
            'cardholder'=>$input['cardholder'],
            'bankcard'=>$input['bankcard'],
            'bankname'=>$input['bankname'],
            'bankprovince'=>$input['hiddenProvince'],
            'bankcity'=>$input['bankcity'],
            'opencardbank'=>$input['opencardbank'],
        ];

        $result = AgentAccountManagement::endAgent($adminid, $data);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //代理商收款账单导出
    public function exportAgentCollectionAccountWithdrawal(){
//        $cityId = $this->request->get('cityid');
        //获取所有城市的代理商信息
        $result = AgentAccountManagement::exportAgentCollectionAccountWithdrawal();
        $cellData = [['代理区域','持卡人','银行卡号','银行名称','开户行名称信息']];
        foreach($result['data'] as $list) {
            $newList = [
                'cityname' =>$list['cityname'],
                'cardholder' => $list['cardholder'],
                'bankcard' => $list['bankcard'],
                'bankname' => $list['bankname'],
                'bankprovince' => $list['bankprovince'].' - '.$list['bankcity'].' - '.$list['opencardbank'],
            ];
            array_push($cellData, $newList);
        }
        $title = '代理商收款账号列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('代理商收款账号列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //代理商对账单
    public function agentStatement() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $perpage = 200;
        $result = AgentAccountManagement::agentStatement($startTime,$lastTime);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $data['cityid'] = $cityId;
        $data['starttime'] = $startTime;
        $data['lasttime'] = $lastTime;
        $data['type'] = !empty($type)?$type:'';
        return view('console.finance.agent_statement', $data);
    }
    //导出代理商对账单
    public function exportAgentStatement() {
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
//            $startTime = $startTime . " 00:00:00";
//            $lastTime = $lastTime . " 23:59:59";
            $startTime = $startTime;
            $lastTime = $lastTime;
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $result = AgentAccountManagement::exportAgentStatement($startTime,$lastTime);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $cellData = [['代理类型','代理城市','订单笔数','订单消费金额(元)','[代理商]服务费收益(元)','[代理商]已使用的红包(元)','代理商收入(元)','已提现金额(元)','待提现金额(元)']];
        foreach($result['data'] as $list) {
            $newList = [
                'citytype' =>'市代',
                'cityname' => $list['cityname'],
                'ordercount' => $list['ordercount'],
                'orderamount' => $list['orderamount'],
                'fee' => $list['fee'],
                'redenvelopeamount'  =>$list['redenvelopeamount'],
                'agentprofit'  =>$list['agentprofit'],
                'waitwithdrawalamount'  =>$list['withdrawalamount'],
                'withdrawalamount'  =>$list['waitwithdrawalamount'],
            ];
            array_push($cellData, $newList);
        }
        $title = '代理商对账单列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('代理商对账单列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');

    }
    //代理商对账单明细
    public function agentStatementDetails() {
        $cityListId = $this->request->get('agentcityid');
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $result = AgentAccountManagement::agengtStatementDetails($cityListId,$startTime,$lastTime);
        $data = $result['data'];
        $data['cityid'] = $cityListId;
        $data['lasttime'] = $lastTime;
        $data['starttime'] = $startTime;
        return view('console.finance.agengt_statement_details',$data);
    }
    //导出代理商明细
    public function exportAgentStatementDetails() {
        $cityListId = $this->request->get('agentcityid');
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = $startTime . " 00:00:00";
            $lastTime = $lastTime . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $result = AgentAccountManagement::exportAgentStatementDetails($cityListId,$startTime,$lastTime);
        $cellData = [['日期','订单消费金额(元)','[代理商]服务费收益(元)','[代理商]已使用的红包(元)','收入(元)','体现状态','提现日期']];
        foreach($result['data'] as $list) {
            $newList = [
                'days' =>$list['days'],
                'orderAmount' => $list['orderAmount'],
                'fee' => $list['fee'],
                'redEnvelopeAmount' => $list['redEnvelopeAmount'],
                'agentProfit' => $list['agentProfit'],
                'statusname'  =>$list['statusname'],
                'accounttime'  =>$list['accounttime'],
            ];
            array_push($cellData, $newList);
        }
        $title = '代理商对账单明细列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('代理商对账单明细列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //代理商待提现
    public function agentShopWaitWithdrawal() {
        $cityId = $this->request->get('cityid');
        $result = AgentAccountManagement::waitWithdrawal();
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        $data['cityid'] = $cityId;
        return view('console.finance.agent_shopwaitwithdrawal', $data);
    }
    //待提现打款
    public function agentShopWaitWithdrawalRemit(){
        $withdrawalId = $this->request->get('withdrawalid');
        $cityId = $this->request->get('cityid');
        $agentCityId = $this->request->get('agentcityid');
        $password = $this->request->get('password');
        if(env('WEB_URL') == 'https://www.fbw-china.com/') {
            if ($password != "fbw1984") {
                return $this->responseError("密码错误");
            }
        } else {
            if ($password != "fbw123") {
                return $this->responseError("密码错误");
            }
        }
        $result = AgentAccountManagement::agentShopWaitWithdrawalRemit($withdrawalId, $agentCityId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //代理商待提现导出
    public function exportAgentShopWaitWithdrawal(){
        $result = AgentAccountManagement::exportAgentShopwaitwithdrawal();
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        $title = ["代理城市","持卡人","银行卡号","银行名称","开户行所在地","开户行名称","提现金额(元)"];
        $cellData = [];
        array_push($cellData, $title);
        foreach($data as $list) {
            $newList = [
                'cityname' => $list['cityname'],
                'cardholder' => $list['cardholder'],
                'bankcard' => $list['bankcard'],
                'bankname' => $list['bankname'],
                'address' => !empty($list['bankprovince'])?$list['bankprovince'].'-'.$list['bankcity']:'',
                'opencardbank' => $list['opencardbank'],
                'accountamount' => $list['waitdrawalamount']
            ];
            array_push($cellData, $newList);
        }
        $title = '代理商待提现列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('代理商待提现列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    //代理商已提现
    public function agentShopWithdrawal() {
        $result = AgentAccountManagement::withdrawal();
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        return view('console.finance.agent_shopwithdrawal', $data);
    }
    //代理商已提现导出
    public function exportAgentShopwithdrawal(){
        $result = AgentAccountManagement::exportAgentShopwithdrawal();
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        $title = ["结算月份","代理城市","持卡人","银行卡号","银行名称","开户行所在地","开户行名称","提现金额(元)"];
        $cellData = [];
        array_push($cellData, $title);
        foreach($data as $list) {
            $newList = [
                'accountmonth'=>$list['accountmonth'],
                'cityname' => $list['cityname'],
                'cardholder' => $list['cardholder'],
                'bankcard' => $list['bankcard'],
                'bankname' => $list['bankname'],
                'address' => !empty($list['bankprovince'])?$list['bankprovince'].'-'.$list['bankcity']:'',
                'opencardbank' => $list['opencardbank'],
                'accountamount' => $list['accountamount']
            ];
            array_push($cellData, $newList);
        }
        $title = '代理商已提现列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('代理商已提现列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
}